<template>
  <el-dialog :title="title" :visible.sync="show" size="tiny" :before-close="cancel" :close-on-click-modal="false">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="公司名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="公司简介" :label-width="formLabelWidth" prop="companyDes">
          <el-input
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 20}"
            placeholder="请输入公司简介"
            v-model="form.companyDes">
          </el-input>
      </el-form-item>
      <el-form-item label="公司类型" :label-width="formLabelWidth">
        <el-select v-model="form.companyType" placeholder="请选择活动区域">
          <el-option label="集团内部公司" :value="1"></el-option>
          <el-option label="合作友商" :value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit" >确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { create, update } from 'api/cooperationPlatform'
import { formRules } from '../config'
import { postLog } from 'api/common/index.js'
export default {
  props: ['detail'],
  data () {
    return {
      show: true,
      title: this.detail ? '修改合作公司' : '添加合作公司',
      form: {
        name: this.detail ? this.detail.name : '',
        companyDes: this.detail ? this.detail.companyDes : '',
        companyType: this.detail ? this.detail.companyType : 1
      },
      rules: {},
      formLabelWidth: '120px'
    }
  },
  created () {
    this.rules = {...formRules}
  },
  methods: {
    submit () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.detail) {
            this.revise();
          } else {
            this.create();
          }
        } else {
          this.$alert('请按要求输入内容', '提示', {
            confirmButtonText: '确定'
          })
          return false;
        }
      })
    },
    cancel () {
      this.$emit('close');
    },
    async revise () {
      try {
        let param = {...this.form};
        param.id = this.detail.id;
        let back = await update(param);
        back = back.data;
        if (back.resultCode === '200') {
          postLog('BUSINESS_MANAGEMENT_EDIT', '', param.id, param)
          this.cancel()
        } else {
          this.$alert(back.msg);
        }
      } catch (err) {
        this.$alert('请求出错', '提示');
      }
    },
    async create () {
      try {
        let back = await create(this.form);
        back = back.data;
        if (back.resultCode === '200') {
          postLog('BUSINESS_MANAGEMENT_CREATE', '', '', this.form)
          this.cancel()
        } else {
          this.$alert(back.msg);
        }
      } catch (err) {
        this.$alert('请求出错', '提示');
      }
    }
  }
}
</script>
<style lang="less" scoped>

</style>
